<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.order_total.php 487 2014-01-30 16:30:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 487 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 16:30:11 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class order_total {
	var $modules;
	var $total_values;
	var $values_from_cart;

	function __construct() {
    $this->values_from_cart = false;
		if(defined('MODULE_ORDER_TOTAL_INSTALLED') && not_null(MODULE_ORDER_TOTAL_INSTALLED)) {
			$this->modules = explode(';', MODULE_ORDER_TOTAL_INSTALLED);
			$modules = $this->modules;
			sort($modules);
			reset($modules);
			while (list (, $value) = each($modules)) {
				require_once (DIR_WS_LANGUAGES.$_SESSION['language'].'/modules/order_total/'.$value);
				require_once (DIR_WS_MODULES.'order_total/'.$value);
				$class = substr($value, 0, strrpos($value, '.'));
				$GLOBALS[$class] = new $class();
			}
			unset($modules);
		}
		
	}

	function __destruct() {}

	function credit_selection() {
		$selection_string = '';
		$amount_string = '';
		$class_desc = str_replace(' ','_',TABLE_HEADING_CREDIT);
		$start_string = '<div class="'.strtolower($class_desc).'">';
		$close_string = '</div>';
		$credit_class_string = '';
		if (MODULE_ORDER_TOTAL_INSTALLED) {			
			$header_string = '<h4>'.TABLE_HEADING_CREDIT.'</h4>'."\n";
			reset($this->modules);
			$output_string = '';
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if ($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class) {
					$use_credit_string = $GLOBALS[$class]->use_credit_amount();
					if ($selection_string == '')
						$selection_string = $GLOBALS[$class]->credit_selection();
					if (($use_credit_string != '') || ($selection_string != '')) {
						$output_string = '<strong>'.$GLOBALS[$class]->header.($GLOBALS[$class]->getAmount() > 0 ? ' = <span id="'.$GLOBALS[$class]->code.'_amount">'.$GLOBALS[$class]->getAmount().'</span>':'').'</strong><br /> '.$use_credit_string;
						$output_string .= "\n";
						$output_string .= $selection_string;
					}
				}
			}
			if ($output_string != '') {
				$output_string = $start_string.$header_string.$output_string;
				$output_string .= $close_string;
			}
		}
		return $output_string;
	}

	// update_credit_account is called in checkout process on a per product basis. It's purpose
	// is to decide whether each product in the cart should add something to a credit account.
	// e.g. for the Gift Voucher it checks whether the product is a Gift voucher and then adds the amount
	// to the Gift Voucher account.
	// Another use would be to check if the product would give reward points and add these to the points/reward account.
	//
	function update_credit_account($i) {
		if (MODULE_ORDER_TOTAL_INSTALLED) {
			reset($this->modules);
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if (($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class)) {
					$GLOBALS[$class]->update_credit_account($i);
				}
			}
		}
	}
	// This function is called in checkout confirmation.
	// It's main use is for credit classes that use the credit_selection() method. This is usually for
	// entering redeem codes(Gift Vouchers/Discount Coupons). This function is used to validate these codes.
	// If they are valid then the necessary actions are taken, if not valid we are returned to checkout payment
	// with an error
	//
	function collect_posts() {
		if (MODULE_ORDER_TOTAL_INSTALLED) {
			reset($this->modules);
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if (($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class)) {
					$post_var = 'c'.$GLOBALS[$class]->code;
					if ($_POST[$post_var]) {
						$_SESSION[$post_var] = $_POST[$post_var];
					}
					$GLOBALS[$class]->collect_posts();
				}
			}
		}
	}
	// pre_confirmation_check is called on checkout confirmation. It's function is to decide whether the
	// credits available are greater than the order total. If they are then a variable (credit_covers) is set to
	// true. This is used to bypass the payment method. In other words if the Gift Voucher is more than the order
	// total, we don't want to go to paypal etc.
	//
	function pre_confirmation_check() {
		global $order;
		if (MODULE_ORDER_TOTAL_INSTALLED) {
			$total_deductions = 0;
			reset($this->modules);
			$order_total = $order->info['total'];
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				$order_total = $this->get_order_total_main($class, $order_total);
				if (($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class)) {
					$total_deductions = $total_deductions + $GLOBALS[$class]->pre_confirmation_check($order_total);
					$order_total = $order_total - $GLOBALS[$class]->pre_confirmation_check($order_total);
				}
			}
			if ($order->info['total'] - $total_deductions <= 0) {
				$_SESSION['credit_covers'] = true;
			} else { // belts and suspenders to get rid of credit_covers variable if it gets set once and they put something else in the cart
				unset ($_SESSION['credit_covers']);
			}
		}
	}
	// this function is called in checkout process. it tests whether a decision was made at checkout payment to use
	// the credit amount be applied aginst the order. If so some action is taken. E.g. for a Gift voucher the account
	// is reduced the order total amount.
	//
	function apply_credit() {
		if (MODULE_ORDER_TOTAL_INSTALLED) {
			reset($this->modules);
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if (($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class)) {
					$GLOBALS[$class]->apply_credit();
				}
			}
		}
	}
	// Called in checkout process to clear session variables created by each credit class module.
	//
	function clear_posts() {

		if (MODULE_ORDER_TOTAL_INSTALLED) {
			reset($this->modules);
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if (($GLOBALS[$class]->enabled && $GLOBALS[$class]->credit_class)) {
					$post_var = 'c'.$GLOBALS[$class]->code;
					unset ($_SESSION[$post_var]);
				}
			}
		}
	}
	// Called at various times. This function calulates the total value of the order that the
	// credit will be appled aginst. This varies depending on whether the credit class applies
	// to shipping & tax
	//
	function get_order_total_main($class, $order_total) {
		global $credit, $order;
		//      if ($GLOBALS[$class]->include_tax == 'false') $order_total=$order_total-$order->info['tax'];
		//      if ($GLOBALS[$class]->include_shipping == 'false') $order_total=$order_total-$order->info['shipping_cost'];
		return $order_total;
	}
	// ICW ORDER TOTAL CREDIT CLASS/GV SYSTEM - END ADDITION
	// GV Code End

	function process($module='', $recalc = true) {
    global $price;
    if (!empty($this->total_values) && !$recalc)
    	return $this->total_values;
    	
		$order_total_array = array ( );
		$order_total_array['all'] = array(
      'code'        => 'all',
      'title'       => 'laufende Zwischensumme',
      'netto'       => 0,
      'tax'         => array(),
      'brutto'      => 0,
      'text'        => '0,00',
      'value'       => 0,
      'sort_order'  => 0 );

		if (is_array($this->modules)) {
		reset($this->modules);
			foreach($this->modules as $value) {
				$class = substr($value, 0, strrpos($value, '.'));
        if ($module == '' || in_array($class, $module)) {
  				if ($GLOBALS[$class]->enabled) {
						$GLOBALS[$class]->process($this->values_from_cart, $order_total_array);
						if ($class != 'ot_total') {
							$order_total_array['all']['netto'] += $order_total_array[$GLOBALS[$class]->code]['netto'];
							if (isset($order_total_array[$GLOBALS[$class]->code]['tax']) && is_array($order_total_array[$GLOBALS[$class]->code]['tax'])) {
								foreach($order_total_array[$GLOBALS[$class]->code]['tax'] as $t => $v) {
									if(isset($order_total_array['all']['tax'][$t]))
										$order_total_array['all']['tax'][$t]['value'] += $v['value'];
									else
										$order_total_array['all']['tax'][$t] = $v;
								}
							}
							$order_total_array['all']['brutto'] += $order_total_array[$GLOBALS[$class]->code]['brutto'];
							$order_total_array['all']['value'] += $order_total_array[$GLOBALS[$class]->code]['value'];
/*					for ($i = 0, $n = sizeof($GLOBALS[$class]->output); $i < $n; $i ++) {
  						if (not_null($GLOBALS[$class]->output[$i]['title']) && not_null($GLOBALS[$class]->output[$i]['text'])) {
  							$order_total_array[] = array (
                  'code' => $GLOBALS[$class]->code, 'title' => $GLOBALS[$class]->output[$i]['title'], 'text' => $GLOBALS[$class]->output[$i]['text'], 'value' => $GLOBALS[$class]->output[$i]['value'], 'sort_order' => $GLOBALS[$class]->sort_order);
  						}
  					} */
						}
					}
				}
			}
		}
//echo_e($order_total_array, isset($_GET) ? implode('',array_keys($_GET)) : '');
		$this->total_values = $order_total_array;
		return $order_total_array;
	}

	function output($module='') {
		require_once (DIR_FS_INC.'inc.array_sort_by_key.php');
    global $price;

//echo_e($this->modules);
		$output_string = '';
		$order_total_array = $this->process($module);
		array_sort_by_key($order_total_array, 'sort_order');

//echo_e($order_total_array,'E');
		
		foreach($order_total_array as $order_total_item) {
			if ($order_total_item['sort_order'] > 0)
				$output_string .= 
					'<div class="'.$order_total_item['code'].'">'
						.$order_total_item['title'] . ' ' . $order_total_item['text']
					.'</div>';
		}
		return $output_string;
	}

	function pp_output() {
		$output_string = '';
		if (is_array($this->modules)) {
			reset($this->modules);
			while (list (, $value) = each($this->modules)) {
				$class = substr($value, 0, strrpos($value, '.'));
				if ($GLOBALS[$class]->enabled) {
					$size = sizeof($GLOBALS[$class]->output);
					for ($i = 0; $i < $size; $i ++) {
						$output_string[] = array('title'=>$GLOBALS[$class]->output[$i]['title'], 'text'=>$GLOBALS[$class]->output[$i]['text']);
					}
				}
			}
		}
		return $output_string;
	}
}
?>